package cn.cmdr.controller;

import cn.cmdr.pojo.Journal;
import cn.cmdr.pojo.Staff;
import cn.cmdr.service.JournalService;
import cn.cmdr.service.RoleService;
import cn.cmdr.service.StaffService;
import cn.cmdr.vo.ResultVo;
import cn.cmdr.vo.StaffVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/jur/staff")
public class StaffController {
    @Resource
    private StaffService staffService;

    @Resource
    private JournalService journalService;


    @PostMapping("/staffList")
    public ResultVo getStaffList() {
        List<Staff> list = staffService.getStaffList();
        return ResultVo.sucess(list);
    }

    //添加
    @PostMapping("/add")
    @RequiresPermissions("staff:add")
    public ResultVo add(@RequestBody Staff staff) {
        try {
            if(staff.getStaffCode()==null){
                return ResultVo.fail("账号不能为空");
            }
            if(staff.getStaffName()==null){
                return ResultVo.fail("姓名不能为空");
            }
            if(staff.getPassword()==null){
                return ResultVo.fail("密码不能为空");
            }
            String pass = new Md5Hash(staff.getPassword(),"qwer").toString();
            System.out.println(pass);
            staff.setPassword(pass);
            staff.setSalt("qwer");
            Staff cod = staffService.getStaffByCode(staff.getStaffCode());
            if (cod!=null){
                return ResultVo.fail("账号已存在！！！");
            }
            staff.setCreatedDate(new Date());
            staffService.add(staff);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加员工:"+staff.getStaffName());
            journal.setMenuId(7);
            journalService.add(journal);
            return ResultVo.sucess("添加成功！");
        } catch (Exception e) {
            return ResultVo.fail("添加失败！！", e);//包含了打印错误日志
        }
    }

    //修改
    @PostMapping("/update")
    @RequiresPermissions("staff:update")
    public ResultVo update(@RequestBody Staff staff) {
        try {
            Subject subject = SecurityUtils.getSubject();
            Staff staff1 = (Staff) subject.getPrincipal();

            if(staff.getPassword()!=null){
                if(staff.getPassword().length()!=32){
                    String pass = new Md5Hash(staff.getPassword(),"qwer").toString();
                    staff.setPassword(pass);
                }
            }
            staff.setModifyDate(new Date());
            staffService.update(staff);
            if(staff.getId() == staff1.getId()){
                PrincipalCollection principalCollection = subject.getPrincipals();
                String realmName = principalCollection.getRealmNames().iterator().next();
                PrincipalCollection newPrincipalCollection =
                        new SimplePrincipalCollection(staff,realmName);
                subject.runAs(newPrincipalCollection);

            }
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("修改员工:"+staff.getStaffName());
            journal.setMenuId(7);
            journalService.add(journal);
            return ResultVo.sucess("角色修改成功");
        } catch (Exception e) {
            return ResultVo.fail("角色修改失败", e);
        }
    }

    //删除
    @GetMapping("/del/{id}")
    @RequiresPermissions("staff:del")
    public ResultVo del(@PathVariable String id) {
        Staff staff = staffService.getStaffById(Integer.parseInt(id));
        Staff staff1 =  (Staff) SecurityUtils.getSubject().getPrincipal();
        if(staff.getRoleId()!=null){
            if (staff.getRoleId()==3||staff.getId()==staff1.getId()){
                return ResultVo.fail("用户删除失败");
            }
        }
        try {
            staffService.deleteById(Integer.parseInt(id));
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("删除员工:"+staff.getStaffName());
            journal.setMenuId(7);
            journalService.add(journal);
            return ResultVo.sucess("角色删除成功！");
        } catch (Exception e) {
            return ResultVo.fail("角色删除失败！", e);
        }

    }

    //分页
    @PostMapping("/list/{pageSize}/{pageNo}")
    public ResultVo getList(@PathVariable Integer pageSize, @PathVariable Integer pageNo, @RequestBody StaffVo staffVo) {
        return ResultVo.sucess(staffService.getPageStaffList(pageNo, pageSize, staffVo));
    }
    @PostMapping("/getIndexStaff")
    public Object getindexStaff(){
        Staff staff = (Staff) SecurityUtils.getSubject().getPrincipal();
        return staff;
    }
}
